package com.dd.logistics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.logistics.common.Result;
import com.dd.logistics.entity.Customer;
import com.dd.logistics.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    
    @Autowired
    private CustomerService customerService;
    
    @GetMapping("/list")
    public Result<List<Customer>> list() {
        try {
            List<Customer> list = customerService.list();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取客户列表失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/page")
    public Result<Page<Customer>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name) {
        try {
            Page<Customer> page = new Page<>(current, size);
            QueryWrapper<Customer> wrapper = new QueryWrapper<>();
            if (name != null && !name.isEmpty()) {
                wrapper.like("name", name);
            }
            customerService.page(page, wrapper);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("分页查询客户失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/get/{name}")
    public Result<Customer> getCustomerByName(@PathVariable String name) {
        try {
            Customer customer = customerService.getByName(name);
            return customer != null ? Result.success(customer) : Result.error("客户不存在");
        } catch (Exception e) {
            return Result.error("获取客户信息失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/add")
    public Result<Boolean> addCustomer(@RequestBody Customer customer) {
        try {
            if (customer.getName() == null || customer.getName().isEmpty()) {
                return Result.error("客户名称不能为空");
            }
            boolean success = customerService.save(customer);
            return success ? Result.success("添加客户成功", true) : Result.error("添加客户失败");
        } catch (Exception e) {
            return Result.error("添加客户失败：" + e.getMessage());
        }
    }
    
    @PutMapping("/update")
    public Result<Boolean> updateCustomer(@RequestBody Customer customer) {
        try {
            if (customer.getId() == null) {
                return Result.error("客户ID不能为空");
            }
            boolean success = customerService.updateCustomerInfo(customer);
            return success ? Result.success("更新客户信息成功", true) : Result.error("更新客户信息失败");
        } catch (Exception e) {
            return Result.error("更新客户信息失败：" + e.getMessage());
        }
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteCustomer(@PathVariable Long id) {
        try {
            boolean success = customerService.removeById(id);
            return success ? Result.success("删除客户成功", true) : Result.error("删除客户失败");
        } catch (Exception e) {
            return Result.error("删除客户失败：" + e.getMessage());
        }
    }
} 